define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'clothmaking_plan/index/index' + location.search,
                    add_url: 'clothmaking_plan/index/add'+ location.search,
                    edit_url: 'clothmaking_plan/index/edit',
                    del_url: 'clothmaking_plan/index/del',
                    multi_url: 'clothmaking_plan/index/multi',
                    import_url: 'clothmaking_plan/index/import',
                    table: 'clothmaking_plan',
                    dragsort_url: 'ajax/weigh',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                sortOrder: 'desc',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'product.name', title: __('Product.name') },
                        { field: 'producttype', title: __('Producttype'),searchList: { "product": __('TypeProduct'), "raw": __('TypeRaw'), "sample": __('TypeSample') }, formatter: Table.api.formatter.status },                                        
                        { field: 'recipes.title', title: __('recipe.title'), operate: 'LIKE' },
                        { field: 'thickness_min', title: __('Thickness_min') },
                        { field: 'thickness_max', title: __('Thickness_max') },
                        { field: 'spring_min', title: __('Spring_min') },
                        { field: 'spring_max', title: __('Spring_max') },
                        { field: 'grain', title: __('Grain'), formatter: Table.api.formatter.bool },
                        { field: 'csamples', title: __('Csamples'), formatter: Table.api.formatter.bool },
                        { field: 'psamples', title: __('Psamples'), formatter: Table.api.formatter.bool },
                        { field: 'mark', title: __('Mark'), operate: 'LIKE' },
                        { field: 'quantity', title: __('Quantity') },
                        { field: 'deliverytime', title: __('Deliverytime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'status', title: __('Status'), searchList: { "normal": __('Normal'), "hidden": __('Hidden') }, formatter: Table.api.formatter.status },
                        { field: 'weigh', title: __('Weigh'), operate: false },
                        { field: 'user_id', title: __('User_id') },
                        { field: 'product.name', title: __('Product.name'), operate: 'LIKE' },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function() {
       
            $('#c-producttype').on('change', function(){
                var kindname= $(this).val();
            
                console.log(kindname);
                switch(kindname)
                {
                default:                                     
                    $("#c-productid_text").data("selectPageObject").option.data = "clothmaking_product/index";
                    break;                
                case "raw":                    
                    $("#c-productid_text").data("selectPageObject").option.data = "clothmaking_raw/index";
                    break;
                }
                $('#c-productid').selectPageRefresh();

            });
            $("#c-recipe_id").data("params", function (obj) {
                //obj为SelectPage对象
                return {custom: {product_id: $("#c-productid").val(),kind:$('#c-producttype').val()}};
            });

            var orderproduct_id=$("#c-orderproduct_id").val();
           
            Controller.api.bindevent();
         
            
        },
        edit: function() {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});